package com.chunyu.ask.controller.zixun;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chunyu.ask.bean.Article;
import com.chunyu.ask.bean.Channel;
import com.chunyu.ask.bean.Tag;
import com.chunyu.ask.service.ArticleService;
import com.chunyu.ask.service.ChannelService;
import com.chunyu.ask.util.PageUtils;
import com.chunyu.ask.util.RespMsg;

/**
 * 描        述：健康咨询
 * 创建时间：2016-8-19
 * @author Jibaole
 */
@Controller
@RequestMapping("/news")
public class NewsController {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ChannelService channelService;
	/*
	 * 健康咨询-首页
	 */
	@RequestMapping("/")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView(PageUtils.JKZX_LINK);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			//所有——>健康咨询频道
			paramMap.put("firstResult", 0);
			paramMap.put("maxResult", PageUtils.MAX_PAGE);
			paramMap.put("parentId", PageUtils.CHANNEL_ID_JKZX);
			paramMap.put("isValid", 1);//有效
			paramMap.put("sortId", 1);//排序
			
			List<Channel> channelList= channelService.getList(paramMap);
			
			
			if(!CollectionUtils.isEmpty(channelList)){
				StringBuffer sbf = new StringBuffer("");
				//频道列表
				for(Channel obj:channelList){
					sbf.append(obj.getId()+",");
					paramMap.clear();
					paramMap.put("firstResult", 0);
					paramMap.put("maxResult", PageUtils.NEWS_MAX_PAGE);
					paramMap.put("sortType", 1);//时间倒序
					
					paramMap.put("channelId", obj.getId());
					List<Article> articleList = articleService.getList(paramMap);
					view.addObject(obj.getStrId()+"List",articleList);
				}
				//轮播咨询列表
				if(!StringUtils.isEmpty(sbf)){
				String	channelIds = sbf.substring(0, sbf.length()-1);
					paramMap.remove("channelId");
					paramMap.put("maxResult",8);
					paramMap.put("channelIds", channelIds);
					List<Article> bannerList = articleService.getList(paramMap);
					view.addObject("bannerList",bannerList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	
	
    /**
     * 健康咨询内容页
     * @param id
     * @return
     */
	@RequestMapping("/page/{id}")
	public ModelAndView page(@PathVariable int id) {
		ModelAndView view = new ModelAndView(PageUtils.NEWS_DETAIL);
	try {
		Article article = articleService.getById(id);
		view.addObject("article", article);
	} catch (Exception e) {
		e.printStackTrace();
	}
	return view;
   }
	
	
	@RequestMapping("/list/{urlName}/")
	public @ResponseBody
	ModelAndView getList(@PathVariable String urlName,HttpServletRequest req) {
		ModelAndView view = new ModelAndView(PageUtils.NEWS_LIST);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String p = req.getParameter("p");
		try {
			paramMap.put("parentId", PageUtils.CHANNEL_ID_JKZX);
			paramMap.put("isValid", 1);//有效
			paramMap.put("strId", urlName);
			Channel  channelObj = channelService.getChannel(paramMap);
			if(channelObj != null){
				paramMap.clear();
				if(!StringUtils.isEmpty(p)){
					paramMap.put("firstResult", Integer.parseInt(p)*20);
				}
				paramMap.put("channelId", channelObj.getId());
				List<Article> articleList = articleService.getList(paramMap);
				view.addObject("channelId",channelObj.getId());
				view.addObject("title",channelObj.getName());
				view.addObject("articleList", articleList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	
	//#######Android && IOS 接口
	
	/*
	 * 健康咨询-首页
	 */
	@RequestMapping("/jkzx_index_data")
	public @ResponseBody RespMsg Jkzx_v1() {
		RespMsg result = new RespMsg();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			//所有——>健康咨询频道
			paramMap.put("firstResult", 0);
			paramMap.put("maxResult", PageUtils.MAX_PAGE);
			paramMap.put("parentId", PageUtils.CHANNEL_ID_JKZX);
			paramMap.put("isValid", 1);//有效
			paramMap.put("sortId", 1);//排序
			
			List<Channel> channelList= channelService.getList(paramMap);
			
			if(!CollectionUtils.isEmpty(channelList)){
				StringBuffer sbf = new StringBuffer("");
				//频道列表
				for(Channel obj:channelList){
					sbf.append(obj.getId()+",");
					paramMap.clear();
					paramMap.put("firstResult", 0);
					paramMap.put("maxResult", PageUtils.NEWS_MAX_PAGE);
					paramMap.put("sortType", 1);//时间倒序
					paramMap.put("channelId", obj.getId());
					
					List<Article> articleList = articleService.getList(paramMap);
					if(!CollectionUtils.isEmpty(articleList)){
						for(Article o:articleList){
							o.setStrId("/news/list/"+obj.getStrId()+"/");
						}
					}
					resultMap.put(obj.getStrId()+"List",articleList);
				}
				
				//轮播咨询列表
				if(!StringUtils.isEmpty(sbf)){
				String	channelIds = sbf.substring(0, sbf.length()-1);
					paramMap.remove("channelId");
					paramMap.put("maxResult",8);
					paramMap.put("channelIds", channelIds);
					List<Article> bannerList = articleService.getList(paramMap);
					resultMap.put("bannerList",bannerList);
				}
			}
			result.resultObj=resultMap;
			result.status = true;
		} catch (Exception e) {
			result.status = false;
			result.msg="加载咨询首页异常";
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 获取：健康咨询tag标签列表
	 * @return
	 */
	@RequestMapping("/getTagList")
	public @ResponseBody RespMsg getTagList() {
		RespMsg result = new RespMsg();
		List<Tag> tagList = new ArrayList<Tag>();
		try {
			tagList.add(new Tag("/news/list/ylbg/", "八卦"));
			tagList.add(new Tag("/news/list/nxht/", "男性"));
			tagList.add(new Tag("/news/list/nxzt/", "女性"));
			tagList.add(new Tag("/news/list/hyzn/", "育儿"));
			tagList.add(new Tag("/news/list/lnjb/", "老人"));
			tagList.add(new Tag("/news/list/yszd/", "养生"));
			
			result.resultObj=tagList;
			result.status = true;
		} catch (Exception e) {
			result.status = false;
			result.msg="加载经验频道列表异常";
			e.printStackTrace();
		}
		return result;
	}
 }
